var itemIds;
var aindex=1;
$().ready(function() {
    validateRule();

});

$.validator.setDefaults({
    submitHandler : function() {
        getAllSelectNodes();
        save();
    }
});
function save() {
    $('#items').val(itemIds);
    $.ajax({
        cache : true,
        type : "POST",
        url : "/jzweb/classesExaminationPaper/save",
        data : $('#signupForm').serialize(),// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}
function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules : {
            teacherMajorId : {
                required : true
            },
            orgId : {
                required : true
            },
            coursesId : {
                required : true
            },
            classId : {
                required : true
            },
            items : {
                required : true
            },
            name : {
                required : true
            },
            passMark : {
                required : true
            },
            sumMark : {
                required : true
            },
            startTime : {
                required : true
            },
            endTime : {
                required : true
            },
        },
        messages : {
            teacherMajorId : {
                required : icon + "请选择教学专业！"
            },
            orgId : {
                required : icon + "请选择所属机构！"
            },
            coursesId : {
                required : icon + "请选择课程！"
            },
            classId : {
                required : icon + "请选择班级！"
            },
            items : {
                required : icon + "请选择考试题目！"
            },
            name : {
                required : icon + "请输入作业标题！"
            },
            passMark : {
                required : icon + "请输入合格分数！"
            },
            sumMark : {
                required : icon + "请输入总分数！"
            },
            startTime : {
                required : icon + "请输入开始时间！"
            },
            endTime : {
                required : icon + "请输入截止时间！"
            },
        }
    })
}

function getAllSelectNodes() {
    var ref = $('#menuTree').jstree(true); // 获得整个树

    itemIds = ref.get_selected(); // 获得所有选中节点的，返回值为数组

    $("#menuTree").find(".jstree-undetermined").each(function(i, element) {
        itemIds.push($(element).closest('.jstree-node').attr("id"));
    });
}

function getMenuTreeData() {
    if(aindex!=1){
        $('#menuTree').jstree(true).destroy();// 清除树节点
    }
    aindex++;
    let teacherMajorId = $("#teacherMajorId").val();
    $.ajax({
        type : "GET",
        url : "/jzweb/papersDeployment/tree",
        data: {
            id:'',
            teacher_major_id:teacherMajorId
        },
        success : function(menuTree) {
            // console.log("menuTree",menuTree)
            loadMenuTree(menuTree);
        }
    });
}

function loadMenuTree(menuTree) {
    $('#menuTree').jstree({
        'core' : {
            'data' : menuTree
        },
        "checkbox" : {
            "three_state" : true,
        },
        "plugins" : [ "wholerow", "checkbox" ]
    });
    //$('#menuTree').jstree("open_all");
    if(aindex!=1){
        $('#menuTree').jstree(true).refresh(); // 刷新树
    }
}

//选择课程
$("#teacherMajorId").change(function () {
    $('#menuTree').html("");
    getMenuTreeData();
});
//选择课程
$("#orgId").change(function () {
    $("#coursesId").empty();
    $.ajax({
        url: '/jzweb/courses/selCoureses',
        data: {orgIds: $('#orgId').val(), cprofileIds: '', teacherIds:''},
        success: function (r) {
            console.log("er", r)
            if (r.data.length == 0) {
                $("#coursesId").append('<option value="">暂无数据</option>');
            } else {
                $("#coursesId").append('<option value="">请选择</option>');
                for (var p in r.data) {
                    $("#coursesId").append('<option value="' + r.data[p].id + '">' + r.data[p].name + '</option>');
                }
            }
        }
    })
});
//选择班级
$("#coursesId").change(function () {
    $("#classId").empty();
    $.ajax({
        url: '/jzweb/courses/selCouresesClasses',
        data: {coursesIds: $('#coursesId').val(),},
        success: function (r) {
            console.log("er", r)
            if (r.data.length == 0) {
                $("#classId").append('<option value="">暂无数据</option>');
            } else {
                $("#classId").append('<option value="">请选择</option>');
                for (var p in r.data) {
                    $("#classId").append('<option value="' + r.data[p].id + '">' + r.data[p].name + '</option>');
                }
            }
        }
    })
});